package alipay

import (
	"fmt"
	"net/url"
	"strconv"

	"github.com/smartwalle/alipay/v3"
)

func SendTrade(t *AliPayTrade) string {
	// AliSettings 是个自己写的存放参数的结构体, 自己单独写好插进去
	var aliSettings = getSelfAliPaySecrete()
	appID := aliSettings.APPID               // 你的appID
	privateKey := aliSettings.PrivateKey     // 你的私钥
	aliPublicKey := aliSettings.AliPublicKey // 支付宝的公钥

	var client, err = alipay.New(appID, privateKey, false)
	if err != nil {
		panic(err)
	}

	err = client.LoadAliPayPublicKey(aliPublicKey)
	if err != nil {
		panic(err)
	}

	// var p = alipay.TradeWapPay{}
	var p = alipay.TradePagePay{}
	p.NotifyURL = t.NotifyURL
	p.ReturnURL = t.ReturnURL
	p.Subject = t.Subject
	p.OutTradeNo = strconv.Itoa(t.OutTradeNo)   // 传递一个唯一单号
	p.TotalAmount = strconv.Itoa(t.TotalAmount) // 金额
	// p.ProductCode = "QUICK_WAP_WAY"
	p.ProductCode = "FAST_INSTANT_TRADE_PAY" // 网页支付
	var url2 *url.URL
	url2, err = client.TradePagePay(p)
	if err != nil {
		fmt.Println(err)
	}

	var payURL = url2.String()
	fmt.Println(payURL)

	return payURL

}
